package com.itheima.web.service.impl.sms;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.service.sms.ICouponProductCategoryRelationService;
import com.itheima.service.sms.ICouponProductRelationService;
import com.itheima.service.sms.ICouponService;
import com.itheima.vo.sms.Coupon;
import com.itheima.vo.sms.CouponProductCategoryRelation;
import com.itheima.vo.sms.CouponProductRelation;
import com.itheima.vo.sms.CouponVo;
import com.itheima.web.mapper.sms.CouponMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 优惠券表 服务实现类
 */
@DubboService
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    @Autowired
    private ICouponService couponService;
    @Autowired
    private ICouponProductRelationService couponProductRelationService;
    @Autowired
    private ICouponProductCategoryRelationService couponProductCategoryRelationService;

    @Override
    public Page<Coupon> getPage(String name, Integer type, Integer pageNum, Integer pageSize) {
        Page<Coupon> pageInfo=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Coupon> lqw=new LambdaQueryWrapper<>();
        lqw.like(name!=null&&name!="",Coupon::getName,name);
        lqw.eq(type!=null,Coupon::getType,type);
        lqw.orderByDesc(Coupon::getEnableTime);
         couponService.page(pageInfo, lqw);
        return pageInfo;



    }

    @Override
    public CouponVo getOneMess(Long id) {
        Coupon byId = couponService.getById(id);
        CouponVo couponVo=new CouponVo();
        BeanUtils.copyProperties(byId,couponVo);
        LambdaQueryWrapper<CouponProductRelation> lqw1=new LambdaQueryWrapper<>();
        lqw1.eq(CouponProductRelation::getCouponId,id);
        List<CouponProductRelation> list1 = couponProductRelationService.list(lqw1);
        LambdaQueryWrapper<CouponProductCategoryRelation> lqw2=new LambdaQueryWrapper<>();
        lqw2.eq(CouponProductCategoryRelation::getCouponId,id);
        List<CouponProductCategoryRelation> list2 = couponProductCategoryRelationService.list(lqw2);
        couponVo.setProductRelationList(list1);
        couponVo.setProductCategoryRelationList(list2);
        return couponVo;
    }

    @Override
    @Transactional
    public void delete(Long id) {
        LambdaQueryWrapper<CouponProductRelation> lwq1=new LambdaQueryWrapper<>();
        lwq1.eq(CouponProductRelation::getCouponId,id);
        couponProductRelationService.remove(lwq1);
        LambdaQueryWrapper<CouponProductCategoryRelation> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CouponProductCategoryRelation::getCouponId,id);
        couponProductCategoryRelationService.remove(lqw);
    }
}
